module Easel
	module Menu
		class Base
			attr_accessor :menu
			
			def initialize
				@menu = []
      end

			def <<(content)
				@menu << content if content
      end
    end #base class

		class Link
			attr_accessor :title, :url

		  def initialize(title, url)
				@title = title
				@url = url
			end
    end #link class

		class Group < Easel::Menu::Base
			attr_accessor :title

			def initialize(title)
				super()
				@title = title
			end

		end #group class

		class HTML < Easel::Menu::Base

			def to_html
				%q{<ul id="tree">} + process_menu(self.menu){|itm| rvalue << itm} + %q{</ul>}
			end

			private
			
			def process_menu(mnu)
				mnu.collect do |m|
					if m.kind_of?(Easel::Menu::Link)
						easel_link(m) 
					else 
						start_expand_link(m) + process_menu(m.menu) + end_expand_link
					end
				end.to_s
			end

			def easel_link(tml)
				%{<li><a href="#{tml.url}">#{e(tml.title)}</a></li>}
			end

			def e(text)
				CGI::escapeHTML(text)
			end

			def start_expand_link(mgroup)
        <<-LINK
          <li><span style="hitarea collapsable-hitarea"<strong>#{mgroup.title}</strong></span><ul>
        LINK
			end

			def end_expand_link
				"</ul></li>"
			end
		end
	end
end
